package com.ruoyi.amuse.application.service.impl;


import com.ruoyi.amuse.application.domain.ClubApplication;
import com.ruoyi.amuse.application.mapper.ClubApplicationMapper;
import com.ruoyi.amuse.application.service.IClubApplicationService;
import com.ruoyi.amuse.members.domain.ClubMembers;
import com.ruoyi.amuse.members.mapper.ClubMembersMapper;
import com.ruoyi.amuse.members.service.IClubMembersService;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * 团员审批Service业务层处理
 * 
 * @author juwujie
 * @date 2024-07-19
 */
@Service
public class ClubApplicationServiceImpl implements IClubApplicationService
{
    @Autowired
    private ClubApplicationMapper clubApplicationMapper;

    @Autowired
    private IClubMembersService clubMembersService;

    @Autowired
    private ClubMembersMapper clubMembers;


    /**
     * 查询团员审批
     * 
     * @param applicationId 团员审批主键
     * @return 团员审批
     */
    @Override
    public ClubApplication selectClubApplicationByApplicationId(Long applicationId)
    {
        return clubApplicationMapper.selectClubApplicationByApplicationId(applicationId);
    }

    /**
     * 查询团员审批列表
     * 
     * @param clubApplication 团员审批
     * @return 团员审批
     */
    @Override
    public List<ClubApplication> selectClubApplicationList(ClubApplication clubApplication)
    {
        /*
         * 根据登录的账号对成员表中的成员进行进行角色验证，各个社团的社长只能查看各自社团的信息
         * */
//        获取ruoyi当前要登录的账号
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userAccount = loginUser.getSysUser().getUserAccount();
        List<ClubMembers> clubMembers1 = clubMembers.selectClubMembersList(new ClubMembers(null, null, userAccount, null, null));
        for (ClubMembers clubMembers2 : clubMembers1){
            if (clubMembers2.getRoleId() == null){
                continue;
            }
            if (clubMembers2.getRoleId() == 1){
                clubApplication.setClubId(clubMembers2.getClubId());
                clubApplicationMapper.selectClubApplicationList(clubApplication);
            }else {
                continue;
            }
        }

        return clubApplicationMapper.selectClubApplicationList(clubApplication);
    }

    /**
     * 新增团员审批
     * 
     * @param clubApplication 团员审批
     * @return 结果
     */
    @Override
    public int insertClubApplication(ClubApplication clubApplication)
    {

        // 获取当前时间
        LocalDateTime localDateTime = LocalDateTime.now();

        // 将 LocalDateTime 转换为 ZonedDateTime
        java.time.ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());

        // 从 ZonedDateTime 提取 Instant
        java.time.Instant instant = zonedDateTime.toInstant();

        // 将 Instant 转换为 Date
        Date date = Date.from(instant);
        //设置申请时间为当前时间

//        判断学生是否已经加入过社团
        List<ClubMembers> clubMembers = clubMembersService.selectClubMembersList(new ClubMembers(null,clubApplication.getClubId(),clubApplication.getUserAccount(),null,null));

        if (clubMembers.size() != 0){
            System.out.println("该学员已经加入过该社团");
        }
//        判断该学员是否已经申请过相同的社团
        Long clubId = clubApplication.getClubId();
        Long userAccount = clubApplication.getUserAccount();
        List<ClubApplication> clubApplications = selectClubApplicationList(clubApplication);
//        增强循环判断申请的学号和社团是否重叠
        for (ClubApplication clubApplication1 : clubApplications){
            if (clubApplication1.getClubId().equals(clubId) && clubApplication1.getUserAccount().equals(userAccount)){
                System.out.println("该学员已经申请过该社团");
                return 0;
            }
        }

        clubApplication.setApplicationTime(date);
        return clubApplicationMapper.insertClubApplication(clubApplication);
    }

    /**
     * 修改团员审批
     * 
     * @param clubApplication 团员审批
     * @return 结果
     */
    @Override
    public int updateClubApplication(ClubApplication clubApplication)
    {
//        如果申请状态变更为1，则将此学生的信息插入到社团成员表中
        if (clubApplication.getStatus().equals("1")){
            clubMembersService.insertClubMembers( new ClubMembers(null,clubApplication.getClubId(),clubApplication.getUserAccount(),null,null));
        }
        return clubApplicationMapper.updateClubApplication(clubApplication);
    }

    /**
     * 批量删除团员审批
     * 
     * @param applicationIds 需要删除的团员审批主键
     * @return 结果
     */
    @Override
    public int deleteClubApplicationByApplicationIds(Long[] applicationIds)
    {
        return clubApplicationMapper.deleteClubApplicationByApplicationIds(applicationIds);
    }

    /**
     * 删除团员审批信息
     * 
     * @param applicationId 团员审批主键
     * @return 结果
     */
    @Override
    public int deleteClubApplicationByApplicationId(Long applicationId)
    {
        return clubApplicationMapper.deleteClubApplicationByApplicationId(applicationId);
    }
}
